package com.fhz.day006;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * 时间:2022/3/25
 *
 *
 */
public class MyView extends View {

    private static final String TAG = "fengview";
    String text;
    float size;
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    //代码里面new对象的时候
    public MyView(Context context) {
        super(context);
        init(context,null);
    }

    //xml文件调用
    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);

    }

    //带样式
    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    //高版本
    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context,attrs);
    }

    public void init(Context context,AttributeSet set){
        if(set != null){
            TypedArray typedArray = context.obtainStyledAttributes(set, R.styleable.MyView);
             text = typedArray.getString(R.styleable.MyView_text);
             size = typedArray.getDimension(R.styleable.MyView_size, 100);
            typedArray.recycle();

        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int width = 0 ;
        //warp . march . 10dp
        int mode = MeasureSpec.getMode(widthMeasureSpec);
        int size = MeasureSpec.getSize(widthMeasureSpec);
        switch (mode){
            case MeasureSpec.AT_MOST:
                width = 100;
                break;
                case MeasureSpec.EXACTLY:
                    //march 10
                    width = size;
                    break;
                    case MeasureSpec.UNSPECIFIED:
                        break;
        }


        int height = 0;
        int mode1 = MeasureSpec.getMode(heightMeasureSpec);
        int size1 = MeasureSpec.getSize(heightMeasureSpec);
        switch (mode1){
            case MeasureSpec.AT_MOST:
                height = 100;
                break;
            case MeasureSpec.EXACTLY:
                //march 10
                height = size1;
                break;
            case MeasureSpec.UNSPECIFIED:
                break;
        }
        setMeasuredDimension(width,height);

    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        Log.i(TAG, "dispatchTouchEvent: ");
        return super.dispatchTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.i(TAG, "onTouchEvent: ");
        return super.onTouchEvent(event);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        paint.setTextSize(size);
//        canvas.drawText(text,200,200,paint);
    }
}

